#include "head.h"


int main(){
    char port[100];
    bzero(port, sizeof(port));
    getparameter("port",port);
    char ip[100];
    bzero(ip, sizeof(ip));
    getparameter("ip",ip);
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);

    connect(socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));


    /*
     * @Author:yhy
     * @LastAuthot:yhy
     */
    user_t* user = userInit(); 

    client_t* client = init_clients(1);
    client->desc[0].client_time = time(NULL);
    char token[1024] = {0};
    off_t token_len = 0;

    while(1){
        if(user->status_signin == false ){
            printf("(L)og in 登入\t (R)egister 注册\t (E)xit 退出\n");
            char buff[1024];
            memset(buff,0,sizeof(buff));
            ssize_t readNum =  read(STDIN_FILENO,buff,sizeof(buff));
            if((time(NULL) - client->desc[0].client_time) >= 30){
                close(socket_fd);
                int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
                int err_connect = connect(socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
                client->desc[0].client_time = time(NULL);
            }
            if(readNum >= 3){
                continue;
            }
            else{
                char isEnter = '?';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                char choice = buff[0];
                send(socket_fd, &choice, sizeof(char), MSG_NOSIGNAL);
                switch(choice){
                case 'l':case 'L':{
                                      memset(token, 0, sizeof(token));
                                      user->status_signin = sign_in(socket_fd, user, token);
                                      if (!user->status_signin) {
                                          char msg[1024]={0};
                                          snprintf(msg,sizeof(msg),"%s %s",user->user_name,"User sign-in failed");
                                              LOG_MSG(ERROR, msg);
                                      }
                                      char msg[1024]={0};
                                      snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,"login!");
                                      LOG_MSG(INFO,msg);
                                      break;
                                  }
                case 'r':case 'R':{
                                      sign_up(socket_fd);
                                      break;
                                  }
                case 'e':case 'E':{
                                      exit(1);
                                  }
                default:{
                            fprintf(stdout,"%s\n","输入错误");
                        }
                }
            }
        }
        else{
            printf("%s: %s -> ", user->user_name, user->path);
            fflush(stdout);
            command_t* command=(command_t*)calloc(1,sizeof(command_t));

            char cut_buf[100];
            memset(cut_buf, 0, sizeof(cut_buf));
            read(STDIN_FILENO,cut_buf,sizeof(cut_buf));
            if((time(NULL) - client->desc[0].client_time) >= 30){
                close(socket_fd);
                int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
                connect(socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
                client->desc[0].client_time = time(NULL);
            }
            size_t len = strlen(cut_buf);
            if (len > 0 && cut_buf[len - 1] == '\n') {
                cut_buf[len - 1] = '\0';
            }
            // 检查输入是否为空
            if (strlen(cut_buf) == 0) {
                fprintf(stdout, "输入不能为空，请重新输入。\n");
                continue;  // 跳过本次循环
            }
            //存储命令
            const char* path=user->path;
            snprintf(command->buf,sizeof(command->buf),"%s&%s&",
                     cut_buf, path);
            /* printf("cut_buf: '%s'\n", cut_buf); */
            /* printf("path: '%s'\n", path); */

            /* printf("command->buf=%s",command->buf); */

            char* cmd=strtok(cut_buf," \n");
            char* arg[10];
            int arg_count=0;

            while ((arg[arg_count] = strtok(NULL, " \n")) != NULL) {
                //printf("%d token: %s\n", arg_count, arg[arg_count]);
                arg_count++;
            }

            if(strcmp(cmd,"exit")==0){
                close(socket_fd);  // 关闭客户端 socket
                exit(0);           // 客户端退出
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }
            /* //发送命令 */
            /* send(socket_fd,cmd,strlen(cmd),0); */

            /* //发送参数数量 */
            /* send(socket_fd,&arg_count,sizeof(arg_count),0); */

            /* //发送参数 */
            /* for(int i=0;i<arg_count;i++){ */
            /*     send(socket_fd,arg[i],strlen(arg[i]),0); */
            /* } */
            if(strcmp(cmd,"cd")==0){
                if(arg[0] == NULL){
                    continue;
                }
                char isEnter = '!';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                token_len = strlen(token);
                send(socket_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, token, token_len, MSG_NOSIGNAL);
                //printf("%ld: %s\n", token_len, token);

                off_t buf_len = strlen(command->buf);
                send(socket_fd, &buf_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd,command->buf,buf_len,MSG_NOSIGNAL);
                //printf("%ld: %s\n", buf_len, command->buf);
                changedir(user, arg[0], socket_fd);
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }else if(strcmp(cmd,"ls")==0){
                char isEnter = '!';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                token_len = strlen(token);
                send(socket_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, token, token_len, MSG_NOSIGNAL);
                //printf("%ld: %s\n", token_len, token);

                off_t buf_len = strlen(command->buf);
                send(socket_fd, &buf_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd,command->buf,buf_len,MSG_NOSIGNAL);
                //printf("%ld: %s\n", buf_len, command->buf);
                //todo,执行ls函数
                //todo,执行ls函数
                command->types=LS;
                lsCommand(command,user,socket_fd);
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }else if(strcmp(cmd,"puts")==0){
                if(arg[0] == NULL){
                    continue;
                }
                char isEnter = '+';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                //printf("here!\n");
                //todo
                Send * send_message = (Send*)calloc(1,sizeof(Send));
                pthread_mutex_init(&send_message->lock, NULL);
                //              if(send_message){
                //                printf("yes!\n");
                //            }
                pthread_t pid;

                send_message->net_fd = socket_fd;
                send_message->command_type = 0;
                strcpy(send_message->token, token);
                send_message->user = user;
                strcpy(send_message->argx, arg[0]);
                //printf("arg: %s\n", send_message->argx);
                strcpy(send_message->path, path);
                strcpy(send_message->cut_buf, cut_buf);

                pthread_create(&pid,NULL,thread_main,(void*)send_message);
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }else if(strcmp(cmd,"gets")==0){
                if(arg[0] == NULL){
                    continue;
                }
                char isEnter = '+';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                //todo
                Send * send_message = (Send*)calloc(1,sizeof(Send));
                pthread_mutex_init(&send_message->lock, NULL);
                //              if(send_message){
                //                printf("yes!\n");
                //            }
                pthread_t pid;

                send_message->net_fd = socket_fd;
                send_message->command_type = 1;
                strcpy(send_message->token, token);
                send_message->user = user;
                strcpy(send_message->argx, arg[0]);
                //printf("arg: %s\n", send_message->argx);
                strcpy(send_message->path, path);
                strcpy(send_message->cut_buf, cut_buf);

                pthread_create(&pid,NULL,thread_main,(void*)send_message);
                
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }else if(strcmp(cmd,"rm")==0){
                if(arg[0] == NULL){
                    continue;
                }
                char isEnter = '!';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                token_len = strlen(token);
                send(socket_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, token, token_len, MSG_NOSIGNAL);
                //printf("%ld: %s\n", token_len, token);

                off_t buf_len = strlen(command->buf);
                send(socket_fd, &buf_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd,command->buf,buf_len,MSG_NOSIGNAL);
                //printf("%ld: %s\n", buf_len, command->buf);
                //todo
                removefile(socket_fd, arg[0]);
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }else if(strcmp(cmd,"pwd")==0){
                char isEnter = '!';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                token_len = strlen(token);
                send(socket_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, token, token_len, MSG_NOSIGNAL);
                //printf("%ld: %s\n", token_len, token);

                off_t buf_len = strlen(command->buf);
                send(socket_fd, &buf_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd,command->buf,buf_len,MSG_NOSIGNAL);
                //printf("%ld: %s\n", buf_len, command->buf);
                //todo
                command->types=PWD;
                pwdCommand(command,user);
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            }else if(strcmp(cmd,"mkdir")==0){
                if(arg[0] == NULL){
                    continue;
                }
                char isEnter = '!';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                token_len = strlen(token);
                send(socket_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, token, token_len, MSG_NOSIGNAL);
                //printf("%ld: %s\n", token_len, token);

                off_t buf_len = strlen(command->buf);
                send(socket_fd, &buf_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd,command->buf,buf_len,MSG_NOSIGNAL);
                printf("%ld: %s\n", buf_len, command->buf);
                ;
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,cut_buf);
                LOG_MSG(INFO,msg);
            } else{
                char isEnter = '!';
                send(socket_fd, &isEnter, sizeof(char), MSG_NOSIGNAL);
                token_len = strlen(token);
                send(socket_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, token, token_len, MSG_NOSIGNAL);
                //printf("%ld: %s\n", token_len, token);

                off_t buf_len = strlen(command->buf);
                send(socket_fd, &buf_len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd,command->buf,buf_len,MSG_NOSIGNAL);
                //printf("%ld: %s\n", buf_len, command->buf);
                // 处理无效命令
                const char* error_msg = "Error: Invalid command\n";
                /*off_t len = strlen(error_msg);
                send(socket_fd, &len, sizeof(off_t), MSG_NOSIGNAL);
                send(socket_fd, error_msg, len, MSG_NOSIGNAL);*/
                char msg[1024]={0};
                snprintf(msg,sizeof(msg),"user:%s %s",user->user_name,error_msg);
                LOG_MSG(ERROR, msg);
            }    
        }
    }
    close(socket_fd);
    return 0;
}

